package com.gitee.sparta.common.exception;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.gitee.sparta.common.SpartaErrorEnum;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 断言工具，主要是完成逻辑判断，如果未达到
 * 判断条件，则做出相应的异常或者其他处理
 *
 * @author zyred
 * @since v 0.0.1
 */
public class Assert {


    public static void isTrue(boolean bol, SpartaErrorEnum code) {
        if (bol) {
            throw new SpartaException(code);
        }
    }

    public static void isTrue(boolean bol, String message) {
        if (bol) {
            throw new SpartaException(message);
        }
    }

    public static void ne (String tar, String str, SpartaErrorEnum code) {
        if (!Objects.equals(tar, str)) {
            throw new SpartaException(code);
        }
    }

    public static void isNull(Object obj, SpartaErrorEnum code) {
        if (Objects.isNull(obj)) {
            throw new SpartaException(code);
        }
    }

    public static void nonNull (Object non,  SpartaErrorEnum code) {
        if (Objects.nonNull(non)) {
            throw new SpartaException(code);
        }
    }

    public static void isEmpty(Collection<?> obj, SpartaErrorEnum code) {
        if (CollUtil.isEmpty(obj)) {
            throw new SpartaException(code);
        }
    }

    public static void isBlank(String str, SpartaErrorEnum code) {
        if (StrUtil.isBlank(str)) {
            throw new SpartaException(code);
        }
    }

    public static void isNotEmpty (Collection<?> list, SpartaErrorEnum code) {
        if (CollUtil.isNotEmpty(list)) {
            throw new SpartaException(code);
        }
    }

    public static void contains (Integer tar, List<?> elements, SpartaErrorEnum code) {
        if (!elements.contains(tar)) {
            throw new SpartaException(code);
        }
    }

    public static void lessEq(long num, SpartaErrorEnum code) {
        if (num <= 0) {
            throw new SpartaException(code);
        }
    }

    public static void len (String str, int minLen, int maxLen) {
        if (StrUtil.isBlank(str)) {
            return;
        }
        int strLen = str.length();
        // 范围外
        if (strLen < minLen || strLen > maxLen) {
            throw new SpartaException("metaId 范围只能是 " + minLen + " 到 " + maxLen + " 之间");
        }
    }
}
